from dataclasses import dataclass
from enum import IntEnum
from typing import Any


class ResponseCode(IntEnum):
    """响应状态码枚举"""

    SUCCESS = 200
    BAD_REQUEST = 400
    UNAUTHORIZED = 401
    FORBIDDEN = 403
    NOT_FOUND = 404
    INTERNAL_ERROR = 500
    SERVICE_UNAVAILABLE = 503


@dataclass
class Response:
    """基础响应类"""

    code: int
    message: str
    status: Any = None
    data: Any | None = None

    def to_dict(self) -> dict[str, Any]:
        """转换为字典格式，便于序列化"""
        return {"code": self.code, "message": self.message, "data": self.data}

    @classmethod
    def from_dict(cls, data: dict[str, Any]) -> "Response":
        """从字典创建 BaseResponse 对象"""
        return cls(
            code=data["code"], message=data["message"], data=data.get("data", None)
        )
